package com.demo.demo.images;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2023/5/15
 */
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class ImageLabeler {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://pic.616pic.com/photoone/00/07/29/619762ba2cace5236.jpg");
        // Load original image
        BufferedImage originalImage  = ImageIO.read(url);

        // Create a new image with the same width as the original and 320 pixels taller
        BufferedImage labeledImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight() , BufferedImage.TYPE_INT_ARGB);

        // Get the graphics object for the new image
        Graphics2D g2d = labeledImage.createGraphics();

        // Draw the original image onto the new image
        g2d.drawImage(originalImage, 0, 0, null);

        // Fill a translucent black rectangle in the center of the new image
        int labelWidth = 1800;
        int labelHeight = 320;
        int labelX = (labeledImage.getWidth() - labelWidth) / 2;
        int labelY = (labeledImage.getHeight() - labelHeight);
        g2d.setColor(new Color(0, 0, 0, 128));
        g2d.fillRect(0, labelY, labelWidth, labelHeight);

        // Add the label text to the new image
        g2d.setColor(Color.WHITE);
        g2d.setFont(new Font("Arial", Font.BOLD, 24));
        String labelText = "This is my label";
        int stringWidth = g2d.getFontMetrics().stringWidth(labelText);
        int x = labelX + (labelWidth - stringWidth) / 2;
        int y = labelY + ((labelHeight - g2d.getFontMetrics().getHeight()) / 2) + g2d.getFontMetrics().getAscent();
        g2d.drawString(labelText, x, y);

//        // Fill a translucent black rectangle at the bottom of the new image
//        g2d.setColor(new Color(0, 0, 0, 128));
//        g2d.fillRect(0, originalImage.getHeight(), labeledImage.getWidth(), 320);
//
//        // Draw the original image onto the new image
//        g2d.drawImage(originalImage, 0, 0, null);
//
//        // Add the label text to the new image
//        g2d.setColor(Color.WHITE);
//        g2d.setFont(new Font("Arial", Font.BOLD, 24));
//        String labelText = "This is my label";
//        int stringWidth = g2d.getFontMetrics().stringWidth(labelText);
//        int x = (labeledImage.getWidth() - stringWidth) / 2;
//        int y = originalImage.getHeight() + ((320 - g2d.getFontMetrics().getHeight()) / 2) + g2d.getFontMetrics().getAscent();
//        g2d.drawString(labelText, x, y);


        // Save the labeled image
        ImageIO.write(labeledImage, "png", new File("D:\\WorkSpace\\wu\\spring-boot-study\\springboot-demo\\src\\main\\resources\\labeled_image.png"));

        // Clean up
        g2d.dispose();
    }
}

